package ui.button;

import controllers.TabButtonControl;
import ui.panel.WorkPanel;
import utils.CommonUtil;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @version V1.0
 * @Title: Tabbutton，重写了构造方法
 * @Package
 * @Description:
 * @author: GaleBlade
 * @date:
 */
public class TabButton extends JButton {

    public TabButtonControl mouse;
    public int status = 0;
    private WorkPanel workPanel;
    private JPanel jPanel;

    /**
     * 构造方法
     */
    public TabButton(int x, int y, String name, WorkPanel workPanel, JPanel jPanel){
        this.workPanel = workPanel;
        this.jPanel = jPanel;
        super.setLayout(null);
        super.setContentAreaFilled(false);
        super.setIcon(ImageUtils.label);
        super.setBorder(null);
        super.setBounds(x,y,90,30);
        super.setForeground(Color.WHITE);
        JLabel text = new JLabel(name, JLabel.CENTER);
        text.setText(name);
        text.setFont(CommonUtil.getFont());
        text.setForeground(Color.WHITE);
        text.setBounds(0,0,90,30);
        super.add(text);
        //添加鼠标事件
        mouse = new TabButtonControl(this);
        super.addMouseListener(mouse);
        if (y < 50){
            this.addActionListener(new TabButton.clickListenerForParam());
        }else {
            this.addActionListener(new TabButton.clickListenerForResult());
        }

    }

    public int getStatus(){
        return this.status;
    }

    public void setHold(){
        status = 1;
        setIcon(ImageUtils.tab);
    }

    public void setRelease(){
        setIcon(ImageUtils.label);
        status = 0;
    }

    /**
     * hold监听器
     */
    private class clickListenerForParam implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //根据按钮内的状态设置是否开启功能
            if (status == 0){
                changeOthersForParam();
                setIcon(ImageUtils.tab);
                status = 1;
            }else {
                setIcon(ImageUtils.label);
                status = 0;
            }
        }
    }

    /**
     * hold监听器
     */
    private class clickListenerForResult implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //根据按钮内的状态设置是否开启功能
            if (status == 0){
                setIcon(ImageUtils.tab);
                status = 1;
            }else {
                setIcon(ImageUtils.label);
                status = 0;
            }
        }
    }

    /**
     * param区选中tab按钮时切换到对应的panel
     */
    public void changeOthersForParam(){
        workPanel.linesBtn.setIcon(ImageUtils.label);
        workPanel.voiceBtn.setIcon(ImageUtils.label);
        workPanel.clickBtn.setIcon(ImageUtils.label);
        workPanel.linesBtn.status = 0;
        workPanel.voiceBtn.status = 0;
        workPanel.clickBtn.status = 0;
        workPanel.linesPanel.setVisible(false);
        workPanel.voicePanel.setVisible(false);
        workPanel.clickPanel.setVisible(false);
        jPanel.setVisible(true);
    }

}
